package com.gc.jbnfgen;

import com.gc.jbnfgen.gen.*;
import java.io.File;


/**
 *
 * @author dmitry.mamonov
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final JBNF comma = new Constant("comma",",");
        final JBNF dot = new Constant("dot",".");
        final JBNF name = new Value(String.class,"name");
        final JBNF identifier = new Alternative("Identifier"){{
           add("IdentifierShort",name);
           add(new Sequence("IdentifierLong").add("parent",name).add(dot).add("child",name));
        }};

        final JBNF number = new Value(int.class, "Numeric");
        final JBNF string = new Value(String.class, "Symbols");
        final JBNF value = new Alternative("Value")
                .add(identifier)
                .add(number)
                .add(string);
        final JBNF operation = new Alternative("Operation")
                .add(new Constant("plus","+"))
                .add(new Constant("minus","-"))
                .add(new Constant("mul","*"))
                .add(new Constant("div","/"));
        final JBNF bracketOpen = new Constant("BrOpen","(");
        final JBNF bracketClose = new Constant("BrClose",")");
        
        
        final JBNF identifiersList = new Sequence("IdentifiersList"){{
                add(identifier);
                add(new Optional("LongIdentifier"){{
                    add(new Sequence("IdentifierExtention"){{
                        add(comma);
                        add(identifier);
                    }});
                }});
        }};
        final JBNF expression =new Alternative("Expression");
        expression.add(value);
        expression.add(new Sequence("OperationInExpression"){{
                    add("left",expression);
                    add(operation);
                    add("right",expression);
                }});
        expression.add(new Sequence("NestedExpression"){{
                    add("leftBracket", bracketOpen);
                    add(expression);
                    add("rightBracket",bracketClose);
                }});
        final JBNF expressionWithAlias = new Sequence("ExpressionWithAlias"){{
           add(expression);
           add(new Optional("Alias"){{
               add(new Sequence("AliasName"){{
                   add(new Optional("AliasConstant").add(new Constant("as","as")));
                   add(name);
               }});
           }});
        }};

        new JBNFRenderer().render(new File("src"), new String[]{"com","gc","sql","test"},
                identifiersList,
                expression,
                expressionWithAlias,
                operation);
        System.out.println(identifiersList.reset().toString());
        System.out.println(expression.reset().toString());
        System.out.println(expressionWithAlias.reset().toString());
    }
}
